﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Common.Encrypt
{
    /// <summary>
    /// 安全密码
    /// </summary>
    public static class SafePassword
    {
        /// <summary>
        /// 默认秘钥
        /// </summary>
        private static string Key = "6A61750BB30490604B7DF6FD8DA9FA8E";

        /// <summary>
        /// 当前版本
        /// </summary>
        public static string Version = "1";

        /// <summary>
        /// 验证明文密码是否正确
        /// </summary>
        /// <typeparam name="U">唯一键类型</typeparam>
        /// <param name="uniqueValue">唯一键数值</param>
        /// <param name="plaintext">明文密码</param>
        /// <param name="ciphertext">已被加密后的密文密码</param>
        /// <example>
        /// <code>
        /// <![CDATA[
        ///  public void VerifyDemo()
        ///  {
        ///      //唯一标示，这里取的是用户ID
        ///      int userId=1;
        ///      
        ///      //登陆/消费 明文密码
        ///      string password = "123456";
        ///
        ///      //登陆/消费 密文密码
        ///      string code = "10YMyDkGYXQ3FXly1j/82WUE5vLTKKhVsPvwE/rGsjYdE3IbsZi984A==";
        ///      //加密
        ///      bool safePassword = SafePassword.Verify(userId,password,code);
        ///  }
        /// ]]>
        /// </code>
        /// </example>
        /// <returns></returns>
        public static bool Verify<U>(U uniqueValue, string plaintext, string ciphertext)
        {
            string result = Encode(uniqueValue, plaintext);
            //比较密码
            return result.Equals(ciphertext);
        }


        /// <summary>
        /// 加密明文密码
        /// </summary>
        /// <typeparam name="U">唯一键类型</typeparam>
        /// <param name="uniqueValue">唯一键数值</param>
        /// <param name="plaintext">明文密码</param>
        /// <example>
        /// <code>
        /// <![CDATA[
        ///  public void EncodeDemo()
        ///  {
        ///      //唯一标示，这里取的是用户ID
        ///      int userId=1;
        ///      
        ///      //登陆/消费 密码
        ///      string password = "123456";
        ///  
        ///      //加密
        ///      string safePassword = SafePassword.Encode(userId,password);
        ///  }
        /// ]]>
        /// </code>
        /// </example>
        /// <returns>返回被加密后的密文</returns>
        public static string Encode<U>(U uniqueValue, string plaintext)
        {
            Debug.WriteLine("MD5签名：");
            Debug.WriteLine("加密秘钥：" + Key);
            Debug.WriteLine("唯一编码：" + uniqueValue);
            Debug.WriteLine("加密明文：" + plaintext);

            //MD5数据
            string md5Value = string.Concat(Key, uniqueValue, plaintext);
            Debug.WriteLine("签名数据：" + md5Value);

            //MD5
            string md5 = MD5Provider.Encode(md5Value);
            Debug.WriteLine("签名结果：" + md5);

            //AES
            string desKey = string.Concat(Key, uniqueValue);
            Debug.WriteLine("DES秘钥：" + desKey);

            string desString = AESProvider.Encode(md5, desKey);
            Debug.WriteLine("DES结果：" + desString);

            //加版本
            string result = string.Concat(Version, desString);
            Debug.WriteLine("加版本号（最终结果）：" + result);
            return result;
        }
    }
}
